#!/bin/bash
#@Warning maximum default solutions 9, check the #solutions section
#@Warning script won't work correctly if some task is missing (e.g. A B D ..)
#so dont touch tasks
#@Version 1041

#source folder name
src="milanin"
#global contest id - 0(year:2)(day?:2)3
cnt="013163"
#affix - day number within a year
affix="1"

#do not modify this one, it's used for task looping
tasks="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

#where everything lives (u can set it to pwd result if u launch it manually)
rpath="/__data/test/"
#where the contest lives
cpath=$rpath"contest/"
#where the contest source lives
srcpath=$rpath"src/"
#where the example contest lives
expath=$rpath"patterns/testcontest"

echo Contest number $cnt
echo $src

#here we should create appropriate contest folder with subfolders
mkdir $cpath$cnt
mkdir $cpath$cnt/checkers
mkdir $cpath$cnt/checkers/src
mkdir $cpath$cnt/conf
mkdir $cpath$cnt/solutions
mkdir $cpath$cnt/statements
mkdir $cpath$cnt/tests
mkdir $cpath$cnt/var
for i in $tasks
do
	if [ -d $srcpath$src/$i ] ; then
		mkdir $cpath$cnt/solutions/$i$affix
		mkdir $cpath$cnt/tests/$i$affix
	else
		break
	fi
done
cp $expath/conf/serve.cfg $cpath$cnt/conf/serve.cfg
cp -r $expath/var/* $cpath$cnt/var

#here we should copy testlib
for i in $tasks
do
	if [ -e $srcpath$src/$i/check/testlib.h ] ; then
		cp $srcpath$src/$i/check/testlib.h $cpath$cnt/checkers/src/
		break
	fi
done

for i in $tasks
do
	if [ -d $srcpath$src/$i ] ; then
		t=$i$affix
		echo Working with task $t

		#copying statements
		cp $expath/statements/A.xml $cpath$cnt/statements/$t.xml

		#checker
		cp $srcpath$src/$i/check/*.cpp $cpath$cnt/checkers/src/check_$t.cpp
		g++ $cpath$cnt/checkers/src/check_$t.cpp -o $cpath$cnt/checkers/check_$t
		cp $cpath$cnt/checkers/check_$t $cpath$cnt/tests/$t/check

		#solutions
		cp $srcpath$src/$i/brute/brute.cpp $cpath$cnt/solutions/$t/
		cp $srcpath$src/$i/sol/sol.cpp $cpath$cnt/solutions/$t/

		for ((ii=1; ii<10; ii++))
		do
			if [ -d $srcpath$src/$i/sol$ii ] ; then
				cp $srcpath$src/$i/sol$ii/sol$ii.cpp $cpath$cnt/solutions/$t/
			fi
		done

		g++ $cpath$cnt/solutions/$t/sol.cpp -o $cpath$cnt/tests/$t/sol

		#testgen
		g++ $srcpath$src/$i/testgen/testgen.cpp -o $cpath$cnt/tests/$t/testgen

		#valid
		g++ $srcpath$src/$i/valid/valid.cpp -o $cpath$cnt/tests/$t/valid

		#launch tests
		cp ./checkIn.sh $cpath$cnt/tests/$t/

		prevpath=$(pwd)
		cd $cpath$cnt/tests/$t/
		if [ -e ./log.txt ]; then
			rm ./log.txt
		fi
		./checkIn.sh 1>>log.txt 2>>log.txt
		cd $prevpath

		#remove
		rm $cpath$cnt/tests/$t/check
		rm $cpath$cnt/tests/$t/sol
		rm $cpath$cnt/tests/$t/testgen
		rm $cpath$cnt/tests/$t/valid
		rm $cpath$cnt/tests/$t/checkIn.sh
	else
		break
	fi
done

chmod -R 777 $cpath$cnt

cp $expath/testcontest.xml $path/data/contests/$cnt.xml
#sedstr="s/contest id=13163/contest id="
